package org.dromara.common.outsys.xiong.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.dromara.common.outsys.xiong.constant.XiongConstant;
import org.dromara.common.outsys.xiong.domain.*;
import org.dromara.common.outsys.xiong.service.IXiongService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 基金经理
 * @Author: jeecg-boot
 * @Date:   2022-08-20
 * @Version: V1.0
 */
@Service
public class XiongServiceImpl implements IXiongService {

    private static Log logger = LogFactory.get();


    @Override
    public List<XiongFundBaseInfo> getFundBaseInfo(String... codes){

        String codeStr = "";
        for(String code:codes){
            codeStr = codeStr+"," +code;
        }


        Map<String,Object> params  = new HashMap<>();

        params.put("code",codeStr);
        params.put("token", XiongConstant.TOKEN);

        codeStr = codeStr.substring(1,codeStr.length());
        String resStr  = HttpUtil.get(XiongConstant.URL_FUND_BASE_INFO, params);

        logger.info(resStr);

        JSONObject resParams  = JSONObject.parseObject(resStr);


        List<XiongFundBaseInfo> fundBaseInfos = null;

        if (resParams !=null && resParams.getInteger("code").intValue()  == 200){
            JSONArray resList = resParams.getJSONArray("data");

            fundBaseInfos = new ArrayList<>();

            for (int i = 0; i < resList.size(); i++) {

                JSONObject jsonObject = resList.getJSONObject(i);

                XiongFundBaseInfo fundBaseInfo = JSON.toJavaObject(jsonObject, XiongFundBaseInfo.class);

                fundBaseInfos.add(fundBaseInfo);
            }
        }

        return fundBaseInfos;
    }


    @Override
    public XiongFundDetailInfo getFundDetailInfo(String code, Date startDate, Date endDate){


        Map params  = new JSONObject();


        String startDateStr  = DateUtil.formatDate(startDate);
        String endDateStr =  DateUtil.formatDate(endDate);

        params.put("code",code);
        params.put("startDate",startDateStr);
        params.put("endDate",endDateStr);

        params.put("token",XiongConstant.TOKEN);

        String resStr  = HttpUtil.get(XiongConstant.URL_FUND_DETAIL_INFO, params);

        logger.info(resStr);

        JSONObject resParams = JSONObject.parseObject(resStr);


        if (resParams !=null && resParams.getInteger("code").intValue()  == 200){
            JSONObject detailInfo = resParams.getJSONObject("data");
            XiongFundDetailInfo fundDetailInfo = JSON.toJavaObject(detailInfo, XiongFundDetailInfo.class);

            return fundDetailInfo;
        }else {
            return null;
        }

    }
    @Override
    public XiongFundPosition getFundPosition(String code){

        Map params  = new JSONObject();



        params.put("code",code);
        params.put("token",XiongConstant.TOKEN);

        String resStr  = HttpUtil.get(XiongConstant.URL_FUND_POSITION, params);

        logger.info(resStr);
        JSONObject resParams = JSONObject.parseObject(resStr);

        if (resParams !=null && resParams.getInteger("code").intValue()  == 200){

            JSONObject detailInfo = resParams.getJSONObject("data");
            XiongFundPosition fundPosition = JSON.toJavaObject(detailInfo, XiongFundPosition.class);

            JSONArray stockList = detailInfo.getJSONArray("stockList");
            List<XiongFundStock> fundStocks  = new ArrayList<>();
            for (int i = 0; i < stockList.size(); i++) {
                JSONArray jsonArray = stockList.getJSONArray(i);


                XiongFundStock fundStock = new XiongFundStock();
                fundStock.setCode(jsonArray.getString(0));
                fundStock.setName(jsonArray.getString(1));
                fundStock.setWeightRatio(jsonArray.getString(2));
                fundStock.setOwnNum(jsonArray.getString(3));
                fundStock.setOwnScale(jsonArray.getString(4));
//                FundStock fundStock = JSONObject.toJavaObject(jsonObject, FundStock.class);
                fundStocks.add(fundStock);
            }
            fundPosition.setFundStockList(fundStocks);
            return fundPosition;
        }else {
            return null;
        }

    }

    /**
     * 获取股票基础信息
     */
    @Override
    public List<XiongStockInfo> getStockInfo(List<String> codes){
        Map<String,Object> params  = new JSONObject();

        String codeStr = "";
        for(String code:codes){
            codeStr = codeStr+"," +code;
        }

        codeStr  = codeStr.substring(1,codeStr.length());

        params.put("code",codeStr);

        String resStr  = HttpUtil.get(XiongConstant.URL_STOCK_INFO, params);

        JSONObject resParams = JSONObject.parseObject(resStr);

        logger.info(resParams.toJSONString());

        if (resParams !=null && resParams.getInteger("code").intValue()  == 200){


            JSONArray resList = resParams.getJSONArray("data");

            List<XiongStockInfo> stockInfos = new ArrayList<>();

            for (int i = 0; i < resList.size(); i++) {

                JSONObject jsonObject = resList.getJSONObject(i);

                XiongStockInfo stockInfo = JSON.toJavaObject(jsonObject, XiongStockInfo.class);

                stockInfos.add(stockInfo);
            }
            return  stockInfos;

        }else {
            return null;
        }
    }

}
